package com.shop.servlet.cart;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shop.entity.SHOP_CART;
import com.shop.entity.SHOP_PRODUCT;
import com.shop.entity.SHOP_USER;
import com.shop.service.SHOP_CARTDao;
import com.shop.service.SHOP_PRODUCTDao;

/**
 * Servlet implementation class CartAdd
 */
@WebServlet("/cartadd")
public class CartAdd extends HttpServlet {
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		SHOP_PRODUCT p = null;
		String pid = request.getParameter("id");
		String count = request.getParameter("count");
		String url = request.getParameter("url");
		
		HttpSession session = request.getSession();
		
		String isLogin = (String)session.getAttribute("isLogin");
		
		SHOP_USER user =(SHOP_USER)session.getAttribute("name");
		
		if(user!=null && isLogin.equals("1")) {
			
			String uid = user.getUSER_ID();
			
			//通过用户ID和购物车中的商品ID 查看有没有这条记录
			
			SHOP_CART srcsp = SHOP_CARTDao.getCartShop(uid, pid);
			
			if(srcsp != null) {
				int srccount = srcsp.getCart_quantity();
				int newcount = srccount + Integer.parseInt(count);
				
				if(newcount >=5) {
					newcount =5;		
					}
				SHOP_CARTDao.updatenum(srcsp.getCart_id(), newcount);
				
			}else {
				
			if(pid !=null) {
				p = SHOP_PRODUCTDao.selectById(Integer.parseInt(pid));
			}
			
			SHOP_CART cart = new SHOP_CART(
				    0,
					p.getPRODUCT_FILENAME(),
					p.getPRODUCT_NAME(),
					p.getPRODUCT_PRICE(),
				    Integer.parseInt(count),
				    p.getPRODUCT_STOCK(),
				    p.getPRODUCT_ID(),
				    uid,
				    1			
					);		
		SHOP_CARTDao.insert(cart);			
			}
		}else {
			PrintWriter out = response.getWriter();
			out.write("<script>");
			out.write("alter('用户登录失败')");
			out.write("location.href='login.jsp'");
			out.write("</script>");
		    out.close();
		    return;
		}
		
		if(url.equals("z")) {
			response.sendRedirect("showcart");
		}else {
			response.sendRedirect("selectproductview?id="+pid);
		}	
		
	}

}
